package com.snj.twelve;

import java.util.regex.Pattern;

public class PhoneValidatorWithRegex {
    // 定义手机号的正则表达式，目前常见手机号段开头是 1，第二位是 3 - 9，后面跟 9 位数字
    private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
    private static final Pattern PHONE_PATTERN = Pattern.compile(PHONE_REGEX);

    public static boolean isValidPhoneNumber(String phoneNumber) {
        if (phoneNumber == null) {
            return false;
        }
        // 利用正则模式匹配传入的手机号
        return PHONE_PATTERN.matcher(phoneNumber).matches();
    }

    public static void main(String[] args) {
        String validPhone = "13612345678";
        String invalidPhone = "23612345678";
        System.out.println(validPhone + " 是否有效: " + isValidPhoneNumber(validPhone));
        System.out.println(invalidPhone + " 是否有效: " + isValidPhoneNumber(invalidPhone));
    }
}